package com.css.zfzx.sjcj.modules.drill.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.CheckObjFields;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY05Entity;
import com.css.zfzx.sjcj.modules.drill.Entity.vo.QhY05Vo;
import com.css.zfzx.sjcj.modules.drill.repository.QhY02Repository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY05NativeRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY05Repository;
import com.css.zfzx.sjcj.modules.drill.service.QhService;
import com.css.zfzx.sjcj.modules.drill.service.QhY05Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Li Helei
 * @date 2021/1/16 10:44
 */
@Slf4j
@Service
public class QhY05ServiceImpl implements QhY05Service {

	@Autowired
    private QhY05Repository qhY05Repository;
    @Autowired
    private QhY05NativeRepository qhY05NativeRepository;
    @Autowired
    private QhService qhService;
    /*@Override
    public JSONObject queryQhY05s(QhY05QueryParams queryParams) {
        List<QhY05Entity> qhY05List = this.qhY05NativeRepository.listQhY05s(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhY05List);
        return jsonObject;
    }*/
    @Override
    public JSONObject queryQhY05s(String drillId, int curPage, int pageSize,String sort,String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0){
            sort= CheckObjFields.propertyChange(sort);
        }
        Page<QhY05Entity> qhY05Page = this.qhY05NativeRepository.queryQhY05s(drillId, curPage, pageSize,sort,order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhY05Page);
        return jsonObject;
    }

    @Override
    public List<QhY05Entity> findByDrillId(String drillId) {
        return this.qhY05Repository.findByIsValidAndDrillIdOrderByStandardNumber("1", drillId);
    }

    @Override
    public QhY05Entity getQhY05(String id) {
        QhY05Entity qhY05 = this.qhY05Repository.findByIdAndIsValid(id,"1");
         return qhY05;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY05Entity saveQhY05(QhY05Entity qhY05) {
        String uuid = UUIDGenerator.getUUID();
        qhY05.setId(uuid);
        qhY05.setIsValid("1");
        this.qhY05Repository.save(qhY05);
        try {
            qhService.update(qhY05);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY05;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhY05Entity updateQhY05(QhY05Entity qhY05) {
        this.qhY05Repository.save(qhY05);
        try {
            qhService.update(qhY05);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qhY05;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhY05s(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY05Entity> qhY05List = this.qhY05Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY05List) && qhY05List.size() > 0) {
            for(QhY05Entity qhY05 : qhY05List) {
                qhY05.setIsValid("0");
                this.qhY05Repository.save(qhY05);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQhY05(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        List<QhY05Entity> qhY05List = this.qhY05Repository.findAllById(idList);
        if(!PlatformObjectUtils.isEmpty(qhY05List) && qhY05List.size() > 0) {
            for(QhY05Entity qhY05 : qhY05List) {
                this.qhY05Repository.save(qhY05);
            }
        }
    }

    @Override
    public List<QhY05Vo> dataQualityCheck(String drillId) {
        JSONObject res = new JSONObject();
        List<QhY05Vo> qhY05VoList=new ArrayList<>();
        try {
            List<QhY05Entity> qhY05List=qhY05Repository.findByIsValidAndDrillIdOrderByStandardNumber("1", drillId);
            if(!PlatformObjectUtils.isEmpty(qhY05List) && qhY05List.size() > 0) {
                for (QhY05Entity qhY05 : qhY05List) {
                    QhY05Vo qhY05Vo = new QhY05Vo();
                    qhY05Vo.setId(qhY05.getId());
                    StringBuilder errorMsg = new StringBuilder();

                    //检查字段属性
                    Double standardStartDepth=qhY05.getStandardStartDepth();
                    Double standardEndDepth=qhY05.getStandardEndDepth();
                    Double standardNumber=qhY05.getStandardNumber();

                    if (standardEndDepth!=null){
                        if (standardStartDepth<=0){
                            ServerUtil.addErrorMsg(errorMsg, "起始标贯深度", "大于0");
                        }
                        if ((standardStartDepth+"").length()-(standardStartDepth+"").indexOf(".")-1>2) {
                            ServerUtil.addErrorMsg(errorMsg, "起始标贯深度", "小数点后最多两位");
                        }
                    }
                    if (standardEndDepth!=null){
                        if (standardEndDepth<=0){
                            ServerUtil.addErrorMsg(errorMsg, "结束标贯深度", "大于0");
                        }
                        if ((standardEndDepth+"").length()-(standardEndDepth+"").indexOf(".")-1>2) {
                            ServerUtil.addErrorMsg(errorMsg, "结束标贯深度", "小数点后最多两位");
                        }
                    }
                    if (standardNumber!=null){
                        if (standardNumber<=0){
                            ServerUtil.addErrorMsg(errorMsg, "标贯值", "大于0");
                        }
                        if ((standardNumber+"").length()-(standardNumber+"").indexOf(".")-1>2) {
                            ServerUtil.addErrorMsg(errorMsg, "标贯值", "小数点后最多两位");
                        }
                    }

                    String strErrorMsg = errorMsg.toString();
                    if(!strErrorMsg.isEmpty()) {
                        qhY05Vo.setQualityOpinion("Y05的"+strErrorMsg);
                        qhY05Vo.setDataState("1");
                        break;
                    } else {
                        qhY05Vo.setQualityOpinion("系统自动质检通过");
                        qhY05Vo.setDataState("2");
                    }
                    qhY05VoList.add(qhY05Vo);
                }

            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return qhY05VoList;
    }
}
